#ifndef ELINPUTNUMBER_H
#define ELINPUTNUMBER_H

#include "../basic/elcomponent.h"

class QDoubleSpinBox;
class ElIcon;
class ElInputNumber;

// only accessed to ElInputNumber
// TODO: add autoRepeat for button
class ElInputNumberButton : public ElComponent
{
    Q_OBJECT
    friend class ElInputNumber;

private:
    enum Position { Left, Right, Top, Bottom };

    ElInputNumberButton(Position position, ElInputNumber *parent);

    bool event(QEvent *e) override;
    void paintEvent(QPaintEvent *e) override;
    void enterEvent(QEnterEvent *e) override;
    void leaveEvent(QEvent *e) override;
    void mousePressEvent(QMouseEvent *e) override;
    void mouseReleaseEvent(QMouseEvent *e) override;
    void timerEvent(QTimerEvent *e) override;
    void initPalette() override;

    void resetTimer();

    Position _position;
    ElIcon *_icon = nullptr;
    bool _pressed = false;
    QTimer *_delayTimer = nullptr;
    int _repeatDelay = 500;
    int _repeatInteval = 100;
    int _timerId = -1;

signals:
    void clicked(ElInputNumberButton &); // avoid external call
};

class ElInputNumber : public ElComponent
{
    Q_OBJECT

public:
    enum ButtonPosition { LeftRight, TopBottom, None };

    ElInputNumber(QWidget *parent = nullptr);

    QDoubleSpinBox *editor() const;

    ButtonPosition position() const;
    void setPosition(ButtonPosition position);

    static const int Editor_Width;
    static const int Button_Width;
    static const int Init_Height;
    static const int Padding_Lr;
    static const int Padding_Tb;

public slots:
    void plus();
    void minus();

private slots:
    void judgeValue(double val);

protected:
    bool event(QEvent *e) override;
    bool eventFilter(QObject *o, QEvent *e) override;
    void paintEvent(QPaintEvent *e) override;
    void enterEvent(QEnterEvent *e) override;
    void leaveEvent(QEvent *e) override;
    void initPalette() override;

private:
    Q_PROPERTY(ButtonPosition position READ position WRITE setPosition)

    QDoubleSpinBox *_doubleInput = nullptr;
    ElInputNumberButton *_buttons[2];
    ButtonPosition _position = LeftRight;
};

#endif // ELINPUTNUMBER_H
